package com.kplan.plan.dao;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

import com.kplan.plan.dto.BoardDto;
import com.kplan.plan.dto.BoardListDto;
import com.kplan.util.db.DBClose;
import com.kplan.util.db.DBConnection;

public class BoardDao {
	private static BoardDao boardDao;
	
	static{
		boardDao = new BoardDao();
	}
	
	private BoardDao(){}
	
	public static BoardDao getInstance(){
		return boardDao;
	}
	
	public List<BoardListDto> boardlist(){
		List<BoardListDto> list = new ArrayList<BoardListDto>();
		BoardListDto boardDto=null;
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;
		
		try {
			conn = DBConnection.makeConnection();
			String sql = "select bcode, bname \n";
			sql += "from board_list \n";
			System.out.println(sql);
			stmt = conn.createStatement();
			rs=stmt.executeQuery(sql);
			while(rs.next()){
				boardDto = new BoardListDto();
				boardDto.setBcode(rs.getString("bcode"));
				boardDto.setBname(rs.getString("bname"));
				list.add(boardDto);
				System.out.println(boardDto.getBname());
			}
			
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally{
			DBClose.close(conn, stmt, rs);
		}
		
		return list;
	}
	
	public List<BoardDto> board(String bcode){
		List<BoardDto> list = new ArrayList<BoardDto>();
		BoardDto bDto=null;
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;
		
		try {
			conn = DBConnection.makeConnection();
			String sql = "select b.seq, b.nick, b.email, b.subject, b.content, b.hit, b.logtime, b.bcode, l.bname \n";
			sql += "from board b, board_list l \n";
			sql += "where b.bcode='"+bcode+"' \n";
			sql += "and b.bcode = l.bcode \n";
			System.out.println(sql);
			stmt = conn.createStatement();
			rs=stmt.executeQuery(sql);
			while(rs.next()){
				bDto = new BoardDto();
				bDto.setSeq(rs.getInt("seq"));
				bDto.setNick(rs.getString("nick"));
				bDto.setName(rs.getString("name"));
				bDto.setEmail(rs.getString("email"));
				bDto.setSubject(rs.getString("subject"));
				bDto.setContent(rs.getString("content"));
				bDto.setHit(rs.getInt("hit"));
				bDto.setLogtime(rs.getString("logtime"));
				bDto.setBcode(rs.getInt("bcode"));
				bDto.setBname(rs.getString("bname"));

				list.add(bDto);
				
			}
			
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally{
			DBClose.close(conn, stmt, rs);
		}
		
		return list;
	}
}
